package com.aube.interact.fighter;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.aube.interact.fighter.control.DataManager;
import com.huyn.baseframework.dynamicload.video.model.GoodsModel;
import com.huyn.baseframework.utils.Utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by hill on 16/11/4.
 */

public class GoodsStoreLayout extends RelativeLayout {
    private String TAG = "Aube/GoodsStoreView";
    private Context mContext;

    private RelativeLayout mRoot;
    private LinearLayout mGoodsStoreItemLy;
    private ImageView mShopStoreClose;
    private View mBetterCloseView;

    private IGoodsDetailRevealAnimator mGoodsRevealAnimator;

    public GoodsStoreLayout(Context context) {
        this(context, null);
    }

    public GoodsStoreLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GoodsStoreLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.aube_intera_layout_goodstore, this);
        mRoot = this;

        mGoodsStoreItemLy = (LinearLayout) findViewById(R.id.goodstore_itemly);
        mBetterCloseView = findViewById(R.id.goodstore_bettercloseview);

        mShopStoreClose = (ImageView) findViewById(R.id.goodsstore_close);
        mShopStoreClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(GONE);
                if(mGoodsRevealAnimator != null)
                    mGoodsRevealAnimator.resumeFromCartList();
            }
        });

        mBetterCloseView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(GONE);
                if(mGoodsRevealAnimator != null)
                    mGoodsRevealAnimator.resumeFromCartList();
            }
        });

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.Log(Utils.LogType.DEBUG, TAG, "GoodsStoreView onClick: Block");
            }
        });
    }

    public void fillData(List<GoodsModel> dataList, IGoodsDetailRevealAnimator animator) {
        DataManager.getInstance(mContext).updateGoodsStore(dataList);
        this.mGoodsRevealAnimator = animator;

        Iterator<Map.Entry<Integer, GoodsModel>> iterator = DataManager.getInstance(mContext).getSortedGoodsIterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, GoodsModel> entry = iterator.next();
            final GoodsModel item = entry.getValue();
            if(item.goodsVo != null) {
                final CartItemView view = new CartItemView(mContext);
                view.fillData(item, this, mGoodsRevealAnimator);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.rightMargin = Utils.dip2px(mContext, 25);
                mGoodsStoreItemLy.addView(view, params);
            }
        }
    }

    public void refreshCartState() {
        if(mGoodsStoreItemLy != null) {
            for(int i=0; i<mGoodsStoreItemLy.getChildCount(); i++) {
                CartItemView item = (CartItemView) mGoodsStoreItemLy.getChildAt(i);
                item.update();
            }
        }
    }

    /**
     * 渐现动画
     */
    public void startShowAnim() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f);
        animator.setDuration(400);
        animator.setTarget(mRoot);
        animator.start();
    }

    /**
     * 渐隐动画
     */
    public void startHideAnim() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "alpha", 1.0f, 0.0f);
        animator.setDuration(400);
        animator.setTarget(mRoot);
        animator.start();
    }

    public void dismiss() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "alpha", 1.0f, 0.0f);
        animator.setDuration(400);
        animator.setTarget(mRoot);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                setVisibility(View.GONE);
            }
        });
        animator.start();
    }

    public interface IDataInflater {
        public void fillData(List<GoodsModel> goodsModelList);
    }
}
